#ifndef FMMTCIR
#define FMMTCIR

#include <GL/glut.h>
#include <math.h>

#include "sistema.h"
#include "ranhuras.h"
#include "miscfunc.h"
#include "menufcir.h"
#include "messages.h"

extern sistema ms;
extern fmmt estator, rotor, resul;
extern ranhuras est, rot;

int janfmmtcir;
bool showfmmtcir=false, firstfmmtcir=true;
extern bool ok, step;
extern bool showcmps,showres;

void fmmtcirDisplay(void)
{
	double y;

	glClearColor (PRETO,1.0);
	glClear (GL_COLOR_BUFFER_BIT);
	
	glColor3f(COR_FERROM);
	circle(DISK,245);
	
	glColor3f(COR_RANHURAS);
	est.drawrancir();
	
	glColor3f(COR_AGAP);
	circle(DISK,199);

	glColor3f(COR_FERROM);
	circle(DISK,195);
	
	glColor3f(COR_RANHURAS);
	rot.drawrancir();

	glLineWidth(3);y=235;
	
	if(showcmps)
	{
		glColor3f(COR_VETOR_FE);
		estator.drawfmmtcir(197);
		glBegin(GL_QUADS);
			glVertex2f(-245, y);
			glVertex2f(-245, y+10);
			glVertex2f(-235, y+10);
			glVertex2f(-235, y);
		glEnd();

		glColor3f(COR_VETOR_FR);
		rotor.drawfmmtcir(197);
		glBegin(GL_QUADS);
			glVertex2f(-245, y-20);
			glVertex2f(-245, y-10);
			glVertex2f(-235, y-10);
			glVertex2f(-235, y-20);
		glEnd();

		glColor3f(LEG_COLOR);
		outtext(LEFT, LEG_FONT, -225, y, MSG64B);
		outtext(LEFT, LEG_FONT, -225, y-20, MSG64C);
	}

	if(showres)
	{
		glColor3f(COR_VETOR_FRES);
		resul.drawfmmtcir(197);
		glBegin(GL_QUADS);
			glVertex2f(130, y);
			glVertex2f(130, y+10);
			glVertex2f(140, y+10);
			glVertex2f(140, y);
		glEnd();

		glColor3f(LEG_COLOR);
		outtext(LEFT, LEG_FONT, 150, y, MSG64D);
	}

	glutSwapBuffers();
}

void fmmtcirReshape (int w, int h)
{
	glutReshapeWindow(TAM_J1_X, TAM_J1_Y);
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glOrtho (0.0, (GLdouble) TAM_J1_X, 0.0, (GLdouble) TAM_J1_Y, -1, 1);
	glTranslatef((GLdouble) TAM_J1_X/2, (GLdouble) TAM_J1_Y/2, 0);
}

void fmmtcirKeyboard(unsigned char key, int x, int y)
{
	switch (key) 
	{
	case 9:
		glutFullScreen();
		break;
	case 'r':
		vetoresReshape(TAM_J1_X,TAM_J1_Y);
		break;
	case ' ':
		ok=!ok;
		break;
	case 13:
		step=true;
		break;
	case 'p':
		ms.report();
		break;
	case 'e':
		ms.setIEXC(ms.iexc+2.5);
		controleRedisplay();
		break;
	case 'd':
		ms.setIEXC(ms.iexc-2.5);
		controleRedisplay();
		break;
	case 'q':
		ms.setPT(ms.pt+100000);
		controleRedisplay();
		break;
	case 'a':
		ms.setPT(ms.pt-100000);
		controleRedisplay();
		break;
	case 't':
		ms.go(20000);
		break;
	case 27:
		glutSetWindow(janfmmtcir);
		glutHideWindow();
		showfmmtcir=false;
		break;
	}
}

int fmmtcirCreate(void)
{
    double m; int p;

	est.init(220,200);
	rot.init(195,175);
	
	estator.setampl(20);
	rotor.setampl(20/ms.xs);
	calcres(&estator, &rotor, &m, &p);
	resul.setampl((float)m);
	resul.setphase(p);

	glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (0, 0);
    janfmmtcir=glutCreateWindow (MSG64A); 

    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_FLAT);

	glutDisplayFunc(fmmtcirDisplay);
    glutReshapeFunc(fmmtcirReshape);
    glutKeyboardFunc(fmmtcirKeyboard);
	fmmtcirCreateMenu();
    return 0;
}

#endif